/**
 * 
 */
package com.lemote.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lemote.core.Page;
import com.lemote.exception.AppException;
import com.lemote.service.BaseService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * @author wuhui
 * 
 * 下午04:10:59
 */
@Component("baseAction")
public abstract class BaseAction<T, EntityService extends BaseService<T, Long>>
		extends ActionSupport implements ModelDriven<T>, Preparable,
		ServletRequestAware {

	private static final long serialVersionUID = -1653204626115064950L;

	/** 进行增删改操作后,以redirect方式重新打开action默认页的result名. */
	public static final String RELOAD = "reload";
	
	public static final String GLOBALSUCCESS = "globalSuccess";

	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected Integer pageSize = 15;

	protected Page<T> page = new Page<T>(pageSize);

	protected EntityService entityService;

	protected Long id;
	
	/**
	 * 批量删除id 格式为 1-2-3-4
	 */
	protected String ids;

	protected HttpServletRequest request;

	/**
	 * Action函数,显示Entity列表界面. 建议return SUCCESS.
	 */
	public abstract String list() throws AppException;

	/**
	 * Action函数,显示新增或修改Entity界面. 建议return INPUT.
	 */
	@Override
	public abstract String input() throws AppException;

	/**
	 * Action函数,新增或修改Entity. 建议return RELOAD.
	 */
	public abstract String save() throws AppException;

	/**
	 * Action函数,删除Entity. 建议return RELOAD.
	 */
	public abstract String delete() throws AppException;

	/**
	 * 实现空的prepare()函数,屏蔽了所有Action函数都会执行的公共的二次绑定.
	 */
	public void prepare() throws AppException {
	}

	/**
	 * 定义在input()前执行二次绑定.
	 */
	public void prepareInput() throws AppException {
		prepareModel();
	}

	/**
	 * 定义在save()前执行二次绑定.
	 */
	public void prepareSave() throws AppException {
		prepareModel();
	}

	/**
	 * 等同于prepare()的内部函数,供prepardMethodName()函数调用.
	 */
	public abstract void prepareModel() throws AppException;

	public Page<T> getPage() {
		return page;
	}

	public void setPage(Page<T> page) {
		this.page = page;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public abstract void setEntityService(EntityService entityService);
}
